package DBConnection;

import java.util.List;

import model.entities.Statistics;

import org.hibernate.HibernateException;

public class StatisticsDB extends HibernateUtil implements
		ConnectDB<Statistics, Boolean, Integer> {

	@Override
	public Boolean add(Statistics Statistics) {
		try {
			if (Statistics != null) {
				this.beginTranSaction();
				this.getCurrentSession().save(Statistics);
				this.commitTransaction();
			}
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
		return true;
	}

	@Override
	public Boolean delete(Integer ID) {
		Statistics Statistics = find(ID);
		try {
			if (Statistics != null) {
				this.beginTranSaction();
				this.getCurrentSession().delete(Statistics);
				this.commitTransaction();
			}
		} catch (HibernateException e) {
			this.rollback();
			throw e;
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Statistics> findAll() {
		try {
			this.beginTranSaction();
			return this.getCurrentSession()
					.createCriteria(Statistics.class).list();
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}

	@Override
	public Boolean update(Statistics Statistics) {
		try {
			if (Statistics != null) {
				this.beginTranSaction();
				this.getCurrentSession().update(Statistics);
				this.commitTransaction();
			}
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
		return true;
	}

	@Override
	public Statistics find(Integer ID) {
		try {
			this.beginTranSaction();
			return (Statistics) this.getCurrentSession().get(
					Statistics.class, ID);
		} catch (Exception e) {
			throw e;
		}
	}
	public static void main(String[] args) {
		List<Statistics> ll = new StatisticsDB().findAll();
		System.out.println(ll.size());
		System.out.println(new StatisticsDB().find(1));
	}

}
